<?php

namespace common\models\business;

use common\models\sys\SysOperation;
use Yii;

/**
 * This is the model class for table "business_adress".
 *
 * @property string $id
 * @property string $name
 * @property string $parentid
 * @property integer $type
 */
class BusinessAdress extends \yii\db\ActiveRecord
{
    const TYPE_SUPER = 0;

    /**
     *  国
     */
    const TYPE_GUO = 1;
    /**
     *  省
     */
    const TYPE_SHENG = 2;
    /**
     *  市
     */
    const TYPE_SHI = 3;
    /**
     *  县
     */
    const TYPE_XIAN = 4;
    /**
     * 乡镇
     */
    const TYPE_ZHEN = 5;
    /**
     * 村
     */
    const TYPE_CUN = 6;
    /**
     * 公众号类型列表
     * @var array
     */
    public static $types = [
        self::TYPE_SUPER=>'顶级',
        self::TYPE_GUO => '国',
        self::TYPE_SHENG => '省',
        self::TYPE_SHI => '市',
        self::TYPE_XIAN => '县',
        self::TYPE_ZHEN => '镇',
        self::TYPE_CUN => '村'
    ];



    const STATUS_ACTIVE = 1;
    const STATUS_HIDDEN = 0;

    public static $statues = [
        self::STATUS_ACTIVE => '启用',
        self::STATUS_HIDDEN => '不启用'
    ];



    //主要用于日志记录
    public $uselog = [
        'tabcomment' => '地址',      //用于修改了什么的显示
        'mainfield' => 'name',      //主要的显示标题的字段
        'picturefield' => [],        //图片字段
        'timefield' => [],       //时间字段
        'unsetfiled' =>[],      //不需要记录的字段
    ];



    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'business_adress';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['name', 'parentid', 'type'], 'required'],
            [['type','status'], 'integer'],
            [['lat','lng'], 'number'],
            [['lat','lng'], 'default','value'=>0.000],
            [['name', 'parentid'], 'string', 'max' => 255],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'name' => '地址',
            'parentid' => '上级',
            'type' => '类型',
            'lng' => '经度',
            'lat' => '纬度',
            'status' => '状态'
        ];
    }


    /**
     * @param $id
     * @return array|\yii\db\ActiveRecord[]
     * 获取子集
     */
    public static function sons($id)
    {
        return self::find()->where('parentid=:id',[":id"=>$id])->asArray()->all();
    }



    /**
     * @return \yii\db\ActiveQuery
     * 获取地址的上级地址
     */
    public function getParent()
    {
        return $this->hasOne(BusinessAdress::className(),['id'=>'parentid']);
    }


    /**
     * @return \yii\db\ActiveQuery
     * 获取下级地址
     */
    public function getSon()
    {
        return $this->hasMany(BusinessAdress::className(),['parentid'=>'id']);
    }



    /**
     * @return array|\yii\db\ActiveRecord[]
     * 获取所有的城市
     */
    public static function allProvince()
    {
        return self::find()->select('*')->where('type=:type',[':type'=>BusinessAdress::TYPE_SHENG])->asArray()->all();
    }

    /**
     * @return array|\yii\db\ActiveRecord[]
     * 获取所有的城市
     */
    public static function allCity()
    {
        return self::find()->select('*')->where('type=:type',[':type'=>BusinessAdress::TYPE_SHI])->asArray()->all();
    }

    /**
     * @return array|\yii\db\ActiveRecord[]
     * 获取所有的县城
     */
    public static function allCounty()
    {
        return self::find()->select('*')->where('type=:type',[':type'=>BusinessAdress::TYPE_XIAN])->asArray()->all();
    }


    /**
     * @return array|\yii\db\ActiveRecord[]
     * 获取所有的乡镇
     */
    public static function allTown()
    {
        return self::find()->select('*')->where('type=:type',[':type'=>BusinessAdress::TYPE_ZHEN])->asArray()->all();
    }

    /**
     * @return array|\yii\db\ActiveRecord[]
     * 获取所有的村
     */
    public static function allVillage()
    {
        return self::find()->select('*')->where('type=:type',[':type'=>BusinessAdress::TYPE_CUN])->asArray()->all();
    }


    /**
     * 取出所有地址
     */
    public static function allAddress()
    {
        return self::find()->select('*')->asArray()->all();
    }
}
